<template>
  <div style="padding-top: 10px">
    <el-tag type="success" >充值明细</el-tag>
    <!--    表格区域-->
    <el-table
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%"
      border
      stripe
      :header-cell-style="headClass"
      :cell-style="rowClass"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="55"
      />
      <el-table-column
        label="序号"
        type="index"
        :index="indexMethod"
        sortable
      />
      <el-table-column
        prop="money"
        label="充值金额"
        sortable
      />
      <el-table-column
        prop="remark"
        label="备注"
      />
      <el-table-column
        prop="gmtCreate"
        label="创建时间"
        :formatter="formatDate"
        sortable
      />
      <el-table-column
        prop="gmtModified"
        label="修改时间"
        :formatter="formatDate"
        sortable
      />
      <el-table-column label="操作" width="140">
        <template #default="scope">
          <el-button type="primary" size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-popconfirm title="确定删除吗？" @onConfirm="handleDelete(scope.row.id)">
            <template #reference>
              <el-button style="margin-left: 5px" size="mini" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!--    分页-->
    <div style="margin: 10px 0">
      <el-pagination
        :current-page="currentPage"
        :page-sizes="pageSizes"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <!--    修改弹窗-->
    <el-dialog
      :visible.sync="addMoneyVisible"
      width="30%"
      center
      :show-close="false"
    >
      <span slot="title" style="font-weight: bolder;color: goldenrod">修改备注信息</span>
      <el-form ref="addMoneyForm" :model="addMoneyForm" label-width="120px">
        <el-form-item
          label="充值金额"
          prop="money"
        >
          <el-input v-model="addMoneyForm.money"  :disabled="true" style="width: 80%" />
        </el-form-item>
        <el-form-item
          label="备注"
          prop="remark"
        >
          <el-input v-model="addMoneyForm.remark" type="textarea" :rows="3" style="width: 80%" />
        </el-form-item>
        <div style="margin-top: 50px;text-align: center">
          <el-button style="margin-right: 20px" size="big" @click="cancelByAddMoneyForm">取 消</el-button>
          <el-button style="margin-left: 20px" size="big" type="primary" @click="submitByAddMoneyForm('addMoneyForm')">确 定</el-button>
        </div>
      </el-form>

    </el-dialog>
  </div>
</template>

<script>

import { Message } from '_element-ui@2.13.2@element-ui'
import { deleteById, selectRecharge, update } from '@/api/recharge'
import { formatTimer, parseTime } from '@/utils'

export default {
  name: 'RechargeShowTable',
  data() {
    return {
      // 编辑弹窗数据
      addMoneyForm: {},
      // 编辑弹窗
      addMoneyVisible: false,
      // 查询后储存,用于分页查看使用
      memberId: '',
      // 每页查看数量
      pageSizes: [5, 10, 50, 200],
      // 每页显示数量
      pageSize: 5,
      // 总数量
      total: 0,
      // 当前页
      currentPage: 1,
      // 表格数据
      tableData: [],
      // 选中数据
      multipleSelection: []
    }
  },
  mounted() {
  },
  methods: {
    // 编辑弹窗确定
    submitByAddMoneyForm(addMoneyForm) {
      // 发送请求
      this.$refs[addMoneyForm].validate((valid) => {
        if (valid) {
          new Promise((resolve, reject) => {
            update(this.addMoneyForm).then(response => {
              if (response.code === 20000) {
                Message({
                  message: '修改成功!',
                  type: 'success',
                  duration: 3 * 1000
                })
              }
              // 刷新数据页面
              this.addMoneyVisible = false
              this.addMoneyForm = {}
              this.select(this.memberId)
              resolve()
            }).catch(error => {
              reject(error)
            })
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 编辑弹窗取消
    cancelByAddMoneyForm() {
      this.addMoneyVisible = false
      // 刷新
      this.select(this.memberId)
    },
    // 分页查询明细
    select(memberId) {
      this.memberId = memberId
      this.selectPage(this.currentPage, this.pageSize, memberId)
    },
    // 查询代码抽取 工具
    selectPage(currentPage, pageSize, memberId) {
      new Promise((resolve, reject) => {
        // 查询请求
        selectRecharge(currentPage, pageSize, memberId).then(res => {
          // 赋值给页面渲染
          this.tableData = res.data.records
          // 赋值总数
          this.total = res.data.total
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 页码跟序号处理 工具
    indexMethod(index) {
      const currentPage = this.currentPage // 当前页码
      const pageSize = this.pageSize // 每页条数
      return index + 1 + (currentPage - 1) * pageSize // 返回表格序号
    },
    // 日期处理 工具
    formatDate(row, column) {
      // 获取单元格数据
      const data = row[column.property]
      if (data == null) {
        return null
      }
      const dt = new Date(data)
      return dt.getFullYear() + '-' + (dt.getMonth() + 1) + '-' + dt.getDate()
    },
    // 改变每页显示条数触发
    handleSizeChange(val) {
      this.pageSize = val
      this.selectPage(this.currentPage, val, this.memberId)
    },
    // 改变当前页码触发
    handleCurrentChange(val) {
      this.currentPage = val
      this.selectPage(val, this.pageSize, this.memberId)
    },
    // 表头居中显示
    headClass() {
      return 'text-align:center'
    },
    // 表格数据居中显示
    rowClass() {
      return 'text-align:center'
    },
    // 全选或全不选
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 编辑按钮
    handleEdit(row) {
      // 打开新增窗口,改变为编辑
      this.addMoneyVisible = true
      // 赋值编辑窗口
      this.addMoneyForm = row
    },
    // 删除按钮
    handleDelete(id) {
      new Promise((resolve, reject) => {
        deleteById(id).then(response => {
          if (response.code === 20000) {
            Message({
              message: '删除成功!',
              type: 'success',
              duration: 3 * 1000
            })
          }
          // 刷新数据页面
          this.select(this.memberId)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 选中删除
    selectDelete() {
      for (const multipleSelection of this.multipleSelection) {
        this.handleDelete(multipleSelection.id)
      }
    },
    // 导出Excel 工具
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => {
        if (j === 'timestamp') {
          return parseTime(v[j])
        } else {
          return v[j]
        }
      }))
    },
    // 导出Excel
    exportExcel(member) {
      if (this.tableData.length > 0) {
        // 格式化日期
        for (const g of this.tableData) {
          g.gmtCreate = formatTimer(g.gmtCreate)
          g.gmtModified = formatTimer(g.gmtModified)
        }
        import('@/vendor/Export2Excel').then(excel => {
          // 给第一个元素增加个用户名
          this.tableData[0]['memberName'] = member.memberName
          const tHeader = ['会员名称', '充值金额', '备注', '创建时间', '修改时间']
          const filterVal = ['memberName', 'money', 'remark', 'gmtCreate', 'gmtModified']
          const list = this.tableData
          const data = this.formatJson(filterVal, list)
          excel.export_json_to_excel({
            header: tHeader, // 表头 必填
            data, // 具体数据 必填
            filename: '充值数据', // 非必填
            autoWidth: true, // 非必填
            bookType: 'xlsx' // 非必填
          })
        })
      }
    }
  }
}
</script>

<style scoped>
</style>
